import Mock from "mockjs"

Mock.mock("/api/goodslist", "get", {
  status: 200,
  message: "获取商品列表成功",
  "data|3-10": [
    // data|n-m 随机生成 多少个
    {
      "id|+1": 2, // 模拟自增+1
      id: "@increment(1)", // 自增的数字
      name: "@cname()", // 中文名称
      cword: "@cword(2,5)", // 中文字
      price: "@natural(10,100)", // 整数
      img: "@dataImage(64x64)" // 图片
    }
  ]
})

// 为什么有两个Mock.mock() 因为外层Mock 是请求api的，里面Mock生成数据的
Mock.mock("/api/addgoods", "post", function(option) {
  console.log(option)
  return Mock.mock({
    status: 200,
    message: "@word(2,5)"
  })
})

Mock.mock(/\/api\/getgoods\//, "get", function(option) {
  const arr = /\/api\/getgoods\/(\d+)/.exec(option.url)
  console.log("arr", arr)
  return Mock.mock({
    status: 200,
    message: "获取商品列表成功",
    data: {
      id: arr[1] - 0,
      name: "@fruit()",
      price: 6,
      count: "@increment(1,10)",
      img: "@dataImage(64x64)"
    }
  })
})
